<template>
  <el-dialog :title="info.title" append-to-body :visible.sync="visible" center width="960px" :before-close="handleCancel">
    <div class="">
      <div class="page-filter mb10 fl">
        <el-form ref="filter" :model="filter" label-width="0">
          <el-form-item class="mr10 mb5 fl">
            <el-input v-model="filter.file_name" placeholder="文件名称" size="small" clearable />
          </el-form-item>
          <el-form-item class="mr10 mb5 fl" style="width: 300px">
            <el-button class="fl" type="primary" size="small" @click="getList()">查 询</el-button>
            <el-upload class="fl"
              style="margin-left: 10px;height: 32px; line-height: 1"
              :limit=1
              ref="upload"
              action="#"
              accept=".*"
              list-type="text"
              :http-request="uploadFile"
              :on-change="fileChange"
              :show-file-list="false"
              >
                <el-button type="primary" size="small">导 入</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <el-table class="search-table" :data="list" border fit highlight-current-row style="width: 100%;">
        <el-table-column label="序号" align="center" width="56px">
          <template slot-scope="{$index}">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="文件名称" width="260px" prop="file_name" />
        <el-table-column label="访问路径" min-width="200px">
          <template slot-scope="{row}">
            <a :href="row.url" target="_blank">{{ row.url }}</a>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" width="140px" prop="create_time" />
        <el-table-column label="操作" align="center" width="120px" fixed="right">
          <template slot-scope="{row}">
            <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    </div>
  </el-dialog>
</template>

<script>
import { files, uploadFile, delFile } from '@/api/file'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      filter: {
        pageNumber: 10,
        pageCurrent: 1,
        pageAll: 1
      },
      list: [],
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this, 'form', {})
        this.$set(this, 'form', this.info.edit)
        this.getList()
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    getList() {
      files({
        ...this.filter,
        ...this.form
      }).then(response => {
        this.list = response.list
        this.filter = response.filter
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    fileChange(){
      this.$refs.upload.clearFiles()
    },
    uploadFile(param) {
      const formData = new FormData()
      formData.append('file', param.file)
      formData.append('e_id', this.form.e_id)
      formData.append('type', this.form.type)
      formData.append('db_type', this.form.db_type)
      uploadFile(formData).then(res => {
        this.getList()
        this.$message.success('导入成功')
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleDelete(e) {
      this.$confirm('此操作将删除附件，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        delFile({
          'db_type': this.form.db_type,
          'ids': e['id'],
          'paths': e['path']
        }).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style scoped>
.title{
  height: 34px;
  line-height: 34px;
  padding-left: 6px;
  font-size: 14px;
  font-weight: 600;
}
</style>
